package com.mall.untils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author lucas
 */
public class Page implements Serializable {

	private static final long serialVersionUID = 1L;

	private static int DEFAULT_PAGE_SIZE = 20;
	
	private static int DEFAULT_SHOWNUM_WIDTH = 5;

	private int pageSize = DEFAULT_PAGE_SIZE;
	
	private int shownumwidth = DEFAULT_SHOWNUM_WIDTH;

	private long start; 

	private Object data; 

	private long totalCount;
	
	private int totalPageCount;
	
	private int currentPageNo;
	
	private boolean nextPage;
	
	private boolean previousPage;
	
	private String[] columnNames;

	public String[] getColumnNames() {
		return columnNames;
	}


	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	}

	@SuppressWarnings("unchecked")
	public Page() {
		this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList());
	}


	public Page(long start, long totalSize, int pageSize, Object data) {
		this.pageSize = pageSize;
		this.start = start;
		this.totalCount = totalSize;
		this.data = data;
		this.totalPageCount = getTotalPageCount();
		this.currentPageNo = getCurrentPageNo();
		this.nextPage = hasNextPage();
		this.previousPage = hasPreviousPage();
	}
	
	public Page(long start, long totalSize, int pageSize, Object data, String[] columnNames) {
		this.pageSize = pageSize;
		this.start = start;
		this.totalCount = totalSize;
		this.data = data;
		this.columnNames = columnNames;
		this.totalPageCount = getTotalPageCount();
		this.currentPageNo = getCurrentPageNo();
		this.nextPage = hasNextPage();
		this.previousPage = hasPreviousPage();
	}

	/**
	 * 页码显示的列表
	 * @return
	 */
	public List<Integer> getShowNumList(){
		int begin = 1;
		int end = totalPageCount;
		List<Integer> listnum = new ArrayList<Integer>();
		if(totalPageCount > shownumwidth){
			int left = currentPageNo-1;
			int right = totalPageCount-currentPageNo;
			int range = shownumwidth/2;
			
			if(left < right){
				if((currentPageNo-range+1) > 1){
					begin = currentPageNo-range+1;
				}
				end = begin + shownumwidth -1;
				end = end > totalPageCount?totalPageCount:end;
			}else{
				if((currentPageNo + shownumwidth - range) < totalPageCount){
					end = currentPageNo+range;
				}
				begin = end - shownumwidth+1;
				begin = begin < 1?1:begin;
			}
			
		}
		
		for(;begin<=end;begin++){
			listnum.add(begin);
		}
		return listnum;
	}
	
	
	
	public long getTotalCount() {
		return this.totalCount;
	}

	public int getTotalPageCount() {
		if (totalCount % pageSize == 0)
			return (int) totalCount / pageSize;
		else
			return (int) totalCount / pageSize + 1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public Object getResult() {
		return data;
	}
	
	public void setData(Object data) {
		this.data = data;
	}


	public int getCurrentPageNo() {
		return (int) start / pageSize + 1;
	}

	public boolean hasNextPage() {
		return this.getCurrentPageNo() <= this.getTotalPageCount() - 1;
	}

	public boolean hasPreviousPage() {
		return this.getCurrentPageNo() > 1;
	}

	protected static int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
	}

	public static int getStartOfPage(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}


	public boolean isNextPage() {
		return nextPage;
	}


	public void setNextPage(boolean nextPage) {
		this.nextPage = nextPage;
	}


	public boolean isPreviousPage() {
		return previousPage;
	}


	public void setPreviousPage(boolean previousPage) {
		this.previousPage = previousPage;
	}


	public int getShownumwidth() {
		return shownumwidth;
	}


	public void setShownumwidth(int shownumwidth) {
		this.shownumwidth = shownumwidth;
	}
	
	
//	public static void main(String args[]){
//		int pagesize = 3;
//		 int startIndex = Page.getStartOfPage(26, pagesize);
//		Page page = new Page(startIndex,100,pagesize,null);
//		
//		int curpage = page.getCurrentPageNo();
//		int pagecount = page.getTotalPageCount();
//		long total = page.getTotalCount();
//		
//		System.out.println("curpage:" + curpage);
//		System.out.println("pagecount:" +  pagecount);
//		System.out.println("total:" + total);
//		
//		List<Integer> list = page.getShowNumList();
//		
//		for(Integer i : list){
//			System.out.print(i + ",");
//		}
//		
//	}
	
}
